package com.j2205.hongzhiyuan.controller;

import com.alibaba.fastjson.JSON;
import com.j2205.hongzhiyuan.pojo.Person;
import com.j2205.hongzhiyuan.service.PersonService;
import com.j2205.hongzhiyuan.service.impl.PersonServiceImpl;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class PersonController extends BaseServlet{
    public void addPerson(HttpServletRequest req, HttpServletResponse resp) {
        //1.从前端获取值
        String pName = req.getParameter("txtUName");
        String idCard = req.getParameter("txtUidentity");
        String pTel = req.getParameter("txtUTel");
//        String pType = req.getParameter("ptName");
        String ptID = req.getParameter("Fruit");
        String pPass = req.getParameter("txtUPWD");
        String organize = req.getParameter("txtUteamname");
        //2.封装会员信息
        Person person = new Person();
//        PersonType personType = new PersonType();
//        personType.setPtName(pType);
        person.setPName(pName);
        person.setIdCard(idCard);
        person.setPTel(pTel);
        person.setPtID(Integer.parseInt(ptID));
        person.setPPass(pPass);
        person.setOrganize(organize);
//        person.setPersonType(personType);
        //调用业务层
        PersonService personService = new PersonServiceImpl();
        boolean b = personService.addPerson(person);
        if (b) {
            //添加完成跳转到会员信息页面
            System.out.println("添加成功：" + person);
        }
    }

    public void findAllPerson(HttpServletRequest req, HttpServletResponse resp) {
        try {
            PersonService personService = new PersonServiceImpl();
            List<Person> allPerson = personService.findAllPerson();
            //将Java对象转换为Json串
            String jsonString = JSON.toJSONString(allPerson);
            System.out.println(jsonString);
            //设置响应的编码格式
            resp.setContentType("application/json;charset=UTF-8");
            //获取打印流
            PrintWriter writer = resp.getWriter();
            //将JSON数据放入打印流
            writer.write(jsonString);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void findPersonById(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            PersonService personService = new PersonServiceImpl();
            Person person = personService.findPersonById(Integer.parseInt(id));
            //将Java对象转换为Json串
            String jsonString = JSON.toJSONString(person);
            System.out.println(jsonString);
            //设置响应的编码格式
            resp.setContentType("application/json;charset=UTF-8");
            //获取打印流
            PrintWriter writer = resp.getWriter();
            //将JSON数据放入打印流
            writer.write(jsonString);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void updPerson(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String pid = req.getParameter("pid");
            String pName = req.getParameter("txtUName");
            String idCard = req.getParameter("txtUidentity");
            String pTel = req.getParameter("txtUTel");
            String ptID = req.getParameter("Fruit");
            String organize = req.getParameter("txtUteamname");
            Person person = new Person();
            person.setPid(Integer.parseInt(pid));
            person.setPName(pName);
            person.setIdCard(idCard);
            person.setPTel(pTel);
            person.setPtID(Integer.parseInt(ptID));
            person.setOrganize(organize);

            PersonService personService = new PersonServiceImpl();
            boolean b = personService.updPerson(person);

            if(b) {
                //4.跳转页面
                resp.sendRedirect("/sys/OrderUserInfo.html");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void delPerson(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            int pid = Integer.parseInt(id);
            PersonService personService = new PersonServiceImpl();
            boolean b = personService.delPerson(pid);
            resp.setContentType("application/json;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            if(b) {
                writer.write("success");
            } else {
                writer.write("fail");
            }
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
